package com.leyou.item.web;

import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService groupService;
    @Autowired
    private SpecParamService paramService;

    /**
     * 根据商品分类查询规格组
     * @param id 商品分类id
     * @return 规格组集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id")Long id){
        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(groupService.query().eq("category_id", id).list()));
    }
    /**
     * 查询规格参数集合
     * @param categoryId 分类id
     * @param groupId 规格组id
     * @param searching 是否搜索
     * @return 参数集合
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching){
        return ResponseEntity.ok(paramService.queryParams(categoryId, groupId, searching));
    }



    /**
     * 根据分类id查询规格组及组内参数
     * @return 规格组集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySqpcList(@RequestParam("id") Long categoryId){
       return ResponseEntity.ok(groupService.querySpecList(categoryId));
    }

}
